package com.das.mechanic_base.adapter.maintain;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Space;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.plan.PlanListBean;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class X3MaintainPlanAdapter extends RecyclerView.Adapter<X3MaintainPlanAdapter.MainTainHolder> {

    private List<PlanListBean> mList;
    private Context mContext;
    IOnLongClickMainTainPlan iOnLongClickMainTainPlane;

    public X3MaintainPlanAdapter(Context mContext, IOnLongClickMainTainPlan iOnLongClickMainTainPlane) {
        this.mContext = mContext;
        this.iOnLongClickMainTainPlane = iOnLongClickMainTainPlane;
    }

    public void setDataList(List<PlanListBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MainTainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainTainHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_maintain_plan_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainTainHolder holder, int position) {
        placeHolderViewVisiable(holder, position);
        final PlanListBean bean = mList.get(position);
        holder.tv_plan_name.setText(bean.serviceBaseName);
        holder.tv_time.setText(String.valueOf(bean.warningDate) + " " + getWarningTip(bean.warningDate));
        holder.tv_mileage.setText(String.valueOf(bean.nextReplaceCycleKm) + SpHelper.getData(X3Constent.MILEAGEUNIT, "km"));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iOnLongClickMainTainPlane != null) {
                    iOnLongClickMainTainPlane.iOnClickMainTainPlan(bean);
                }
            }
        });
    }

    private String getWarningTip(String date) {
        String tmp = "";
        Date currentDate = new Date(System.currentTimeMillis());
        Date preDate = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            preDate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar currentCalender = Calendar.getInstance();
        Calendar anchorCalender = Calendar.getInstance();
        currentCalender.setTime(currentDate);
        anchorCalender.setTime(preDate);
        int count = X3TimeUtils.getMonthDiff(currentDate, preDate);

        if (currentCalender.get(Calendar.YEAR) > anchorCalender.get(Calendar.YEAR)
                || ((currentCalender.get(Calendar.YEAR) == anchorCalender.get(Calendar.YEAR)) && (currentCalender.get(Calendar.MONTH) >= anchorCalender.get(Calendar.MONTH)))
        ) {
            tmp = "("+mContext.getString(R.string.x3_now_to_work)+")";
        }else{
            tmp = "("+String.format(mContext.getString(R.string.how_many_months),count)+")";
        }
        return tmp;
    }

    private void placeHolderViewVisiable(MainTainHolder holder, int pos) {
        if (pos == 0) {
            holder.top_space.setVisibility(View.GONE);
        } else {
            holder.top_space.setVisibility(View.VISIBLE);
        }

        if (pos == getItemCount() - 1) {
            holder.bottom_div.setVisibility(View.GONE);
        } else {
            holder.bottom_div.setVisibility(View.VISIBLE);
        }
    }

    public interface IOnLongClickMainTainPlan {
        void iOnClickMainTainPlan(PlanListBean obj);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MainTainHolder extends RecyclerView.ViewHolder {
        Space top_space;
        TextView tv_plan_name;
        TextView tv_time;
        TextView tv_mileage;
        Space bottom_space;
        View bottom_div;

        public MainTainHolder(@NonNull View itemView) {
            super(itemView);
            top_space = itemView.findViewById(R.id.top_space);
            tv_plan_name = itemView.findViewById(R.id.tv_plan_name);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_mileage = itemView.findViewById(R.id.tv_mileage);
            bottom_space = itemView.findViewById(R.id.bottom_space);
            bottom_div = itemView.findViewById(R.id.bottom_div);
        }
    }
}
